<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination_model extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getExaminationList($per_page,$limit)
    {
        $limit = " limit {$per_page},{$limit}";
        $sql = "SELECT a.*,huser.username,dept.name FROM tr_examination_list a,tr_admin_user huser,tr_department dept WHERE  a.examinationDept =dept.id and a.userID=huser.id  ORDER BY a.createTime ASC {$limit}";
        $myExamiationTmpList = $this->_geQuerySql($sql);
        if(!empty($myExamiationTmpList)){
            foreach($myExamiationTmpList as $k=>&$v){
                if($v['examinationTime']<=date('Y-m-d')){
                    $v['status'] ="已考";
                }else{
                    $v['status'] ="未考";
                }
                $paramsExamID = array("examinationID"=>$v["examinationID"]);
                $stand  = $this->getStandard($paramsExamID);
                $stand = $this->getStandRate($stand);
                $v['rate'] = $stand['rate'];
                $v['rateTrue'] = $stand['rateTrue'];
            }
        }
        return $myExamiationTmpList;
    }
    /*
     * 获取考试信息
     */
    public function getExaminationInfo($examinationID){
        $sql = "select a.*,b.name from tr_examination_list a ,tr_department b where a.examinationDept=b.id and a.examinationID=".$examinationID;
        $info = $this->_geQuerySql($sql);
        return $info;
    }

    public function searchUserLower($examID,$score){
        $resultTmp =array();
        $sql = "select userID from tr_user_score where examinationID=".$examID." and score<".$score;
        $result = $this->_geQuerySql($sql);
        $resultTmp = array_column($result,'userID');
        return $resultTmp;
    }
    public function searchUserAll($deptID){
        $resultTmp =array();
        $sql = "select AccountID from tr_user where departmentid=".$deptID." and isDel=0";
        $result = $this->_geQuerySql($sql);
        $resultTmp = array_column($result,'AccountID');
        return $resultTmp;
    }

    public function cancel($id){
        $resultTmp =array('examinationCustomer'=>array());
        $resultTmpNew =array();
        $sql = "SELECT b.paperID,b.paperTitle,b.examinationCustomer FROM tr_examination_paper b WHERE  b.examinationID=".$id;
        $userInfo = $this->_geQuerySql($sql);
        $resultTmp['paperTitle']=array_column($userInfo,'paperTitle');
        $resultTmpNew['examinationCustomer']=array_column($userInfo,'examinationCustomer');

        foreach($resultTmpNew['examinationCustomer'] as $k=>$v){
            $acc = json_decode($v,true);
            foreach($acc as $k=>$v){
                $resultTmp['examinationCustomer'][]=$v;
            }
        }
        //删除list paper question score
        $this->db->where("examinationID",$id);
        $this->db->delete("tr_examination_list");

        $this->db->where("examinationID",$id);
        $this->db->delete("tr_examination_paper");

        $this->db->where("examinationID",$id);
        $this->db->delete("tr_examination_question");

        $this->db->where("examinationID",$id);
        $this->db->delete("tr_user_score");

        return $resultTmp;
    }
    public function getUserInfo($id){
        $tmp = array();
        $sql = "select username from tr_user where AccountID in (".implode(',',$id).") ";
        $userInfo = $this->_geQuerySql($sql);
        $tmp = array_column($userInfo,"username");
        return $tmp;
    }

    public function getExaminationListCnt(){
        $sql = "select examinationID  from tr_examination_list";
        $query = $this->db->query($sql);
        $query->num_rows();
        return $query->num_rows();
    }

    public function pjQuestion($examID,$id,$params,$deptID){
        foreach($params as $key=>$value){
            $info = $this->randSqlQuestion($deptID,$key,$value);
            $insert = $this->pjQuestionInsertData($examID,$id,$info);
        }
        return true;
    }
    private function  pjQuestionInsertData($examID,$id,$info){
        $tab = "tr_examination_question";
        $data = array(
            "examinationID"=>$examID,
            "paperID"=>$id
        );
        foreach($info as $key=>$val){
            $data['questionID'] = $val['questionID'];
            $data['questionTile'] = $val['questionTile'];
            $data['questionType'] = $val['questionType'];
            $data['departmentID'] = $val['departmentID'];
            $insert = $this->insertData($tab,$data);
        }
        return true;

    }
    private function randSqlQuestion($deptID,$typeID,$limit,$tmp = array(),$resultTmp = array()){

        if(!isset($tmp)) $tmp = array();
        if(!isset($resultTmp)) $resultTmp = array();
        for($i=0;$i<$limit;$i++){
            $sql = "SELECT t1.*
                      FROM `tr_test_question` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(questionID) FROM `tr_test_question` WHERE tr_test_question.questionType=".$typeID." AND tr_test_question.departmentID=".$deptID.")) AS questionID ) AS t2
                      WHERE t1.questionID >= t2.questionID
                      AND t1.`questionType`=".$typeID." AND t1.`departmentID`=".$deptID."
                      ORDER BY t1.questionID ASC LIMIT 1";
            $result = $this->_geQuerySql($sql);
            if(!empty($result)){
                if(in_array($result[0]['questionID'],$tmp)){
                   $this->randSqlQuestion($deptID,$typeID,1,$tmp,$resultTmp);
                }else{
                    $tmp[] = $result[0]['questionID'];
                    $resultTmp[] = $result[0];
                }
            }
        }
        return $resultTmp;

    }

    public function getDeptName($id){
        $sql = "select name from tr_department where id=".$id;
        $name = $this->_geQuerySql($sql);
        if($name){
            return $name[0]['name'];
        }
        return "未知部门";
    }

    public function getUserID($userName){
        $userID = array("truename"=>"未知员工","AccountID"=>0);
        if(empty($userName)){
           return $userID ;
        }
        $sql = "select AccountID,truename from tr_user where username='".$userName."' and isDel=0";
        $id = $this->_geQuerySql($sql);
        if($id){
            if(!empty($id[0]['AccountID'])) return $id[0];
        }
        return $userID;
    }





    
}

/* End of file Document_model.php */
/* Location: ./application/models/document_model.php */